/**
 * 
 */
package com.ruyipu.forum.activity;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONObject;

import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.LayoutAnimationController;

import com.ruyipu.forum.R;
import com.ruyipu.forum.base.Constants;
import com.ruyipu.forum.base.Header;
import com.ruyipu.forum.base.RFApplication;
import com.ruyipu.forum.interfaces.IItem;

/**
 * @author donald
 * 
 */
public abstract class BaseActivity extends Activity {

	protected LayoutAnimationController lacList;
	protected int iRelation;
	protected Header myHeader;
	protected List<IItem> dataList = new ArrayList<IItem>();
	

	/**
	 * <ol>
	 * <li>选定包含有头部的布局文件</li>
	 * <li>处理传过来的数据</li>
	 * <li>执行super.onCreate(savedInstanceState);</li>
	 * <li>如果需要刷新，refresh(null);</li>
	 * </ol>
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);

		RFApplication.addActivity(this);
		
		Intent intent = getIntent();
		iRelation = intent.getIntExtra(Constants.ACTIVITY_RELATION, Constants.RELATION_SON);

		makeWidget();

		initWidget();

		initListAnimation();
	}

	/**
	 * 刷新
	 * 
	 * @param v
	 */
	public void refresh(View v) {
		dataLoading();
		loadLocalData();
	}

	/**
	 * <ol>
	 * <li>创建头部动作</li>
	 * <li>找到每一个控件</li>
	 * </ol>
	 */
	protected abstract void makeWidget();

	/**
	 * 1.初始化控件 <li></li>
	 */
	protected abstract void initWidget();
	
	protected abstract void loadLocalData();
	
	protected abstract void updateData(JSONObject jsonObject);

//	protected void travelTo(Category item) {
//		Intent intent = new Intent();
//		intent.putExtra(Category.TAG, item);
//		intent.setClassName(this, CategoryActivity.NAME);
//		startActivity(intent);
//	}
//
//	protected void travelTo(Discussion item) {
//		Intent intent = new Intent();
//		intent.putExtra(Discussion.DISCUSSION, item);
//		intent.setClassName(this, DiscussionActivity.NAME);
//		startActivity(intent);
//	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		switch (event.getKeyCode()) {
		case KeyEvent.KEYCODE_BACK:
			goBack(null);
			break;
		case KeyEvent.KEYCODE_MENU:
			break;
		}
		return false;
	}

	/**
	 * 返回
	 * 
	 * @param v
	 */
	public void goBack(View v) {
		dataLoaded();
		RFApplication.finish(this);
		switch(iRelation) {
		case Constants.RELATION_BROTHER:
			overridePendingTransition(R.anim.right_enter, R.anim.right_exit);
			break;
		case Constants.RELATION_SON:
			overridePendingTransition(R.anim.down_enter, R.anim.down_exit);
			break;
		}
	}

	/**
	 * 数据加载完毕
	 */
	protected void dataLoaded() {
		if (null != myHeader) {
			myHeader.dataLoaded();
		}
	}

	/**
	 * 数据加载中
	 */
	protected void dataLoading() {
		if (null != myHeader) {
			myHeader.dataLoading();
		}
	}

	private void initListAnimation() {
		Animation aList = AnimationUtils.loadAnimation(this, R.anim.list);
		lacList = new LayoutAnimationController(aList);
	}
	
	protected abstract class AsyncDownloader extends AsyncTask<Object, Void, JSONObject> {

		@Override
		protected abstract JSONObject doInBackground(Object... params);

		@Override
		protected void onPostExecute(JSONObject jsonObject) {
			updateData(jsonObject);
			dataLoaded();
		}
	}
}
